﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.IO;
using aucn.web.au.code;
using System.Text.RegularExpressions;

namespace aucn.web.au
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GalleryXmlHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string xmlString;
            context.Response.ContentType = "text/xml";
            StringWriterWithEncoding sw = new StringWriterWithEncoding(System.Text.Encoding.UTF8);
            
            XmlWriterSettings st = new XmlWriterSettings();
            st.Encoding = System.Text.Encoding.UTF8;
            st.Indent = true;
            st.OmitXmlDeclaration = false;
            XmlWriter writer = XmlWriter.Create(sw, st);

            writer.WriteStartDocument();
            writer.WriteStartElement("pics");

            string[] files = Directory.GetFiles(context.Server.MapPath("~/au/gallery/images"));
            foreach (string fileName in files)
            {
                if (Regex.IsMatch(fileName, @"\.(jpg|jpeg|png)$"))
                {
                    char[] sep = { '\\' };
                    string[] fn = fileName.Split(sep);
                    writer.WriteStartElement("pic");
                    writer.WriteAttributeString("src", "/au/gallery/images/" + fn[fn.Length - 1]);
                    writer.WriteAttributeString("title", "");
                    writer.WriteEndElement();
                }
            }

            writer.WriteEndElement();
            writer.WriteEndDocument();

            writer.Flush();
            writer.Close();

            xmlString = sw.ToString();
            sw.Close();

            context.Response.Write(xmlString);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
